<%--
  Created by IntelliJ IDEA.
  User: leixiaolong
  Date: 2018/9/13
  Time: 下午6:43
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

<html>
<head>
    <title>采购单列表</title>
    <link rel="stylesheet" href="/static/css/rpt-condition.css" media="all" type="text/css">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/plugins/layui/css/layui.css" media="all"
          type="text/css">
    <style>
        .layui-table-header {
            background-color: #1E9FFF;
        }

        .layui-table thead tr {
            background-color: #1E9FFF;
            color: black;
        }
    </style>
</head>
<body>
<div style="padding: 10px;">
    <div class="layui-form" style="border-color: black; border-style: solid; border-width: 1px;"
         lay-filter="select-condition">
        <div style="height: 20%; margin-top: 15px;">
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">交易起始日期：</label>
                <input type="text" class="layui-input-inline dom-input" id="exch_start_date"
                       placeholder="yyyy-MM-dd">
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">交易结束日期：</label>
                <input type="text" class="layui-input-inline dom-input" id="exch_end_date"
                       placeholder="yyyy-MM-dd">
            </div>

            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">收货起始日期：</label>
                <input type="text" class="layui-input-inline dom-input" id="deli_start_date" placeholder="yyyy-MM-dd">
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">收货结束日期：</label>
                <input type="text" class="layui-input-inline dom-input" id="deli_end_date" placeholder="yyyy-MM-dd">
            </div>

            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">供货商名称：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="supp_item">
                        <option value="" selected>全部</option>
                    </select>
                </div>
            </div>

            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">单据状态：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="order_stat">
                        <option value="" selected>全部</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">审核状态：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="verfy_stat">
                        <option value="" selected>全部</option>
                    </select>
                </div>
            </div>
            <div class="layui-form-item layui-input-inline">
                <label class="layui-form-label">收货仓库：</label>
                <div class="layui-input-inline">
                    <select class="layui-form-select" id="stor_item" lay-filter="stor_item">
                        <option value="" selected>全部</option>
                    </select>
                </div>
            </div>

        </div>

        <div class="layui-form-item" style="float: right; margin-right: 6%; margin-top: -50px;">
            <button class="layui-btn layui-btn-normal layui-btn-radius" lay-submit data-type="search">搜索</button>
            <button type="reset" class="layui-btn layui-btn-warm layui-btn-radius" style="margin-right: 20px;"
                    lay-submit data-type="reset">重置
            </button>
        </div>
        <div style="clear:both"></div>
    </div>

    <div class="layui-btn-group" style="float: right; margin-right: 100px; margin-top: 5px;">
        <button class="layui-btn layui-btn-sm layui-btn-primary" lay-submit data-type="add">新增</button>
        <button class="layui-btn layui-btn-sm layui-btn-primary">导出</button>
    </div>


    <div style=" background-color: white; margin-top: 35px;">
        <table class="layui-hide" id="tab_cred"></table>
    </div>
</div>
</body>
</html>

<script type="text/html" id="rowBar">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script language='javascript' src="${pageContext.request.contextPath}/plugins/layui/layui.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-select.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/component/form-date.js"></script>
<script language='javascript' src="${pageContext.request.contextPath}/static/js/utils/moment.js"></script>

<script>
    layui.use(["form", "element", "jquery", "layer", "laydate", "table"], function () {
        var form = layui.form, element = layui.element, layer = layui.layer, $ = layui.jquery;
        var laydate = layui.laydate, table = layui.table;

        var nowdate = moment().format('YYYY-MM-DD');
        renderDate('exch_start_date', nowdate);
        renderDate('exch_end_date', nowdate);
        renderDate('deli_start_date', nowdate);
        renderDate('deli_end_date', nowdate);

        initStorSelect("stor_item");
        initSuppInfoSelect("supp_item");
        initCodeTableSelect("order_stat", "/base/getPurcOrderStatList", null);
        initCodeTableSelect("verfy_stat", "/base/getVerfyStatList", null);

        var tableIns = table.render({
            id: 'tab_cred',
            elem: '#tab_cred',
            url: '/purc/cred/getPurcCredList',
            type: 'post',
            where: {
                makerId: '',
                exchStartDate: $('#exch_start_date').val(),
                exchEndDate: $('#exch_end_date').val(),
                deliStartDate: $('#deli_start_date').val(),
                deliEndDate: $('#deli_end_date').val(),
                suppCode: $('#supp_item').find('option:selected').val(),
                stat: $('#order_stat').find('option:selected').val(),
                verfyStat: $('#verfy_stat').find('option:selected').val(),
                storCode: $('#stor_item').find('option:selected').val()
            },
            cellMinWidth: 90,
            height: 'full-230',
            cols: [[
                {field: 'seqNo', title: '序号', type: 'numbers', align: 'center'},
                {field: 'id', title: '采购单id', aligin: 'center', hide: true},
                {
                    field: 'purcType', title: '采购类型', align: 'center',
                    templet: function (row) {
                        if (typeof(row.purcType) === 'undefined' || null == row.purcType) {
                            return '-';
                        }
                        switch (row.purcType) {
                            case 1:
                                return '现采';
                            case 2:
                                return '赊购'
                            default:
                                return row.purcType;
                        }
                    }
                },
                {field: 'makerId', title: '制单人id', align: 'center', hide: true},
                {field: 'makerName', title: '制单人', align: 'center'},
                {
                    field: 'makeDate', title: '制单日期', align: 'center', minWidth: 170,
                    templet: function (row) {
                        return new moment(row.ctime).format('YYYY-MM-DD HH:mm:ss');
                    }
                },
                {field: 'organCode', title: '机构id', align: 'center', hide: true},
                {field: 'organName', title: '机构名称', align: 'center'},
                {field: 'exchDate', title: '交易日期', align: 'center'},
                // {field: 'deliDate', title: '收货日期', align: 'center'},
                {
                    field: 'storName', title: '仓库名称', align: 'center',
                    templet: function (row) {
                        if (typeof(row.storName) === 'undefined' || null == row.storName) {
                            return '-';
                        }
                        return row.storName;
                    }
                },
                {field: 'abbrName', title: '供货商简称', align: 'center'},
                // {field: 'contName', title: '供货商联系人', align: 'center', hide: true},
                // {field: 'contPhone', title: '联系电话', align: 'center', hide: true},
                {
                    field: 'digest', title: '摘要', align: 'center',
                    templet: function (row) {
                        if (typeof(row.digest) === 'undefined' || null == row.digest) {
                            return '-';
                        }
                        return row.digest;
                    }
                },

                {
                    field: 'stat', title: '状态', align: 'center',
                    templet: function (row) {
                        switch (row.stat) {
                            case 0:
                                return '待提交';
                            case 1:
                                return '待采购';
                            case 2:
                                return '待收货';
                            case 3:
                                return '已收货';
                            case 4:
                                return '部分收货';
                            case 5:
                                return '未收货';
                            default:
                                return row.purcType;
                        }
                    }
                },
                {
                    field: 'verfyStat', title: '审核状态', align: 'center',
                    templet: function (row) {
                        switch (row.verfyStat) {
                            case 0:
                                return '待提交审核';
                            case 1:
                                return '待审核';
                            case 2:
                                return '审核通过';
                            case 3:
                                return '审核未通过';
                            case 4:
                                return '反审核';
                            default:
                                return row.verfyStat;
                        }
                    }
                },
                {field: 'verfyErrMsg', title: '审核失败原因', align: 'center'},
                {
                    field: 'opertaor', title: '操作', align: 'center', templet: '#rowBar', fixed: 'right'
                }
            ]],
            done: function () {
                // var myDate = new Date();
                // layer.msg("数据渲染完成, date:" + myDate.getSeconds());
            },
            page: {
                limit: 20,
                limits: [10, 20, 50]
            }
        });

        // form.on('submit(btn-add-filt)', function () {
        //     layer.msg("打开新增页面");
        //     layer.open({
        //         type: 2,
        //         title: ['采购单录入', 'text-align: center; font-size: 20px;'],
        //         area: ['1200px', '600px'],
        //         offset: 'auto',
        //         content: '/purc/purc_cred_add'
        //     });
        // });

        var actives = {
            search: function () {
                tableIns.reload({
                    where: {
                        makerId: '',
                        exchStartDate: $('#exch_start_date').val(),
                        exchEndDate: $('#exch_end_date').val(),
                        deliStartDate: $('#deli_start_date').val(),
                        deliEndDate: $('#deli_end_date').val(),
                        suppCode: $('#supp_item').find('OPTION:selected').val(),
                        stat: $('#order_stat').find('option:selected').val(),
                        verfyStat: $('#verfy_stat').find('option:selected').val(),
                        storCode: $('#stor_item').find('option:selected').val()
                    }
                });
            },
            add: function(){
                // layer.msg("打开新增页面");
                layer.open({
                    type: 2,
                    title: ['采购单录入', 'text-align: center; font-size: 20px;'],
                    area: ['94%', '90%'],
                    offset: 'auto',
                    content: '/purc/purc_cred_add'
                });
            },
            reset: function(){
                var nowdate = moment().format('YYYY-MM-DD');
                renderDate('exch_start_date', nowdate);
                renderDate('exch_end_date', nowdate);
                renderDate('deli_start_date', nowdate);
                renderDate('deli_end_date', nowdate);

                $('#stor_item').val('');
                $('#supp_item').val('');
                $('#order_stat').val('');
                $('#verfy_stat').val('');

                form.render();
            }
        };

        form.on('submit', function (data) {
            actives[$(this).data("type")].call(this);
        });

        //监听行工具事件
        table.on('tool', function(obj){
            //获得当前行数据
            var rowdata = obj.data;
            var layEvent = obj.event;//获得当前行事件
            var tr = obj.tr;//获得当前行tr的dom对象

            if(layEvent == 'edit'){//编辑
                const index = layer.open({
                    type: 2,
                    title: ['采购单录入', 'text-align: center; font-size: 20px;'],
                    area: ['94%', '90%'],
                    offset: 'auto',
                    content: '/purc/purc_cred_edit',
                    success: function(layero, index){
                        var iframeEdit = window[layero.find('iframe')[0]['name']];
                        iframeEdit.syncInit(rowdata);
                    }
                });


            }else if(layEvent == 'del'){//删除
                layer.msg("待补充");
            }else{//其他暂不支持
                layer.msg("请等待开放功能");
            }

            //同步更新缓存对应的值
            // obj.update({username:'123', title: 'xxx'});
        });

    });

    function reloadTable() {
        layui.use(['table', 'jquery'], function () {
            var table = layui.table, $ = layui.jquery;

            debugger
            table.reload('tab_cred', {
                where: {
                    makerId: '',
                    exchStartDate: $('#exch_start_date').val(),
                    exchEndDate: $('#exch_end_date').val(),
                    deliStartDate: $('#deli_start_date').val(),
                    deliEndDate: $('#deli_end_date').val(),
                    suppCode: $('#supp_item').find('OPTION:selected').val(),
                    stat: $('#order_stat').find('option:selected').val(),
                    verfyStat: $('#verfy_stat').find('option:selected').val(),
                    storCode: $('#stor_item').find('option:selected').val()
                }
            });
        })
    }
</script>
